<template>
  <div>
    <div>
      <div id="shuju">
        <span class="el-icon-tickets"></span>
        <span> 数据列表</span>
        <span class="right">
          <el-button type="primary" size="small" @click="addfn()"
            >添加</el-button
          ></span
        >
        <span class="clear"></span>
      </div>
      <div class="wei">
        <Suptable
          :tableData="tableData"
          :pagenum="pagenum"
          :pagesize="pagesize"
          :pagesizeList="fenye"
          :total="total"
          :headerData="headerData"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChange"
          :isTag="true"
          :TagName="'启用'"
          :tagProp="'status'"
        >
          <template v-slot:tagSlot="{ row }">
            <div>
              <el-switch
                v-model="row.row.status"
                inactive-color="#13ce66"
                active-color="#ff4949"
                active-value="1"
                inactive-value="0"
                active-text="否"
                inactive-text="是"
                @change="changState(row.row.status, row.row.id)"
              >
              </el-switch>
            </div>
          </template>
          <template v-slot:opera="{ row }">
            <div>
              <el-button
                type="text"
                @click="goEdit('2', row.row.id, row.row)"
                size="small"
                >编辑</el-button
              >
              <el-button type="text" @click="godelete(row.row.id)" size="small"
                >删除</el-button
              >
            </div>
          </template>
        </Suptable>
      </div>
      <div>
        <el-dialog
          :title="dialogtype == '1' ? '新增' : '编辑'"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose"
        >
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="活动名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="开始时间">
              <el-time-picker
                v-model="ruleForm.startTime"
                placeholder="任意时间点"
                value-format="HH:mm:ss"
              >
              </el-time-picker>
            </el-form-item>
            <el-form-item label="结束时间">
              <el-time-picker
                v-model="ruleForm.endTime"
                placeholder="任意时间点"
                value-format="HH:mm:ss"
              >
              </el-time-picker>
            </el-form-item>
            <el-form-item label="启用" v-if="dialogtype == 1">
              <el-switch v-model="ruleForm.status"></el-switch>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >立即创建</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import SupInput from "@/components/SupInput";
import Suptable from "@/components/Suptable";
import { sesList, editlines, addSes, detSes, editSes } from "@/api/sesMock.js";
export default {
  name: "ses",
  data() {
    return {
      dialogtype: 1,
      ruleForm: {
        id: "",
        name: "",
        startTime: "",
        endTime: "",
        status: "",
      },
      rules: {
        name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
      },
      dialogVisible: false,
      tableData: [],
      headerData: [
        {
          prop: "id",
          label: "编号",
        },
        {
          prop: "name",
          label: "秒杀时段名称",
        },
        {
          prop: "startTime",
          label: "每日开始时间",
        },
        {
          prop: "endTime",
          label: "每日结束时间",
        },
      ],
      pagenum: 1,
      pagesize: 5,
      total: 0,
      fenye: [5, 10, 15, 20],
    };
  },
  mounted() {
    let _self = this;
    _self.userList();
  },
  methods: {
    addfn() {
      let _self = this;
      _self.dialogtype=1
      _self.dialogVisible = true;
      _self.ruleForm = {};
    },
    goEdit(type, id, row) {
      console.log(row);
      let _self = this;
      _self.dialogtype = 2;
      _self.dialogVisible = true;
      _self.ruleForm = {
        id: row.id,
        name: row.name,
        startTime: row.startTime,
        endTime: row.endTime,
        status: row.status,
      };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleSizeChange(val) {
      let _self = this;
      _self.pagesize = val;
      _self.userList();
    },
    handleCurrentChange(val) {
      let _self = this;
      _self.pagenum = val;
      _self.userList();
    },
    userList() {
      let _self = this;
      let data = {
        pagenum: _self.pagenum, 
        pagesize: _self.pagesize,
      };
      sesList(data).then((res) => {
        console.log(res);
        if (res.meta.status == 200) {
          _self.tableData = [];
          _self.tableData = res.data._list;
          console.log(_self.tableData);
          _self.total = res.data.total;
        }
      });
    },
    changState(status, id) {
      let _self = this;
      let data = { status: status ? "0" : "1", id };
      editlines(data).then((res) => {
        console.log(res);
        if (res.meta.status == 200) {
          _self.$message(res.meta.msg);
        }
      });
    },
    godelete(id) {
      let _self = this;
      _self
        .$confirm("确定删除吗", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          let data = { id };
          detSes(data).then((res) => {
            // console.log(res);
            if (res.meta.status == 200) {
              _self.$message({
                type: "success",
                message: "删除成功",
              });
              _self.userList();
            } else {
              _self.$message({
                type: "info",
                message: "res.meta.msg",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitForm(formName) {
      let _self = this;
      _self.dialogVisible = false;
      if (_self.dialogtype == 1) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let { name, startTime, endTime, status } = _self.ruleForm;
            let data = {
              name,
              startTime,
              endTime,
              status: status ? "0" : "1",
            };
            addSes(data).then((res) => {
              console.log(res);
              if (res.meta.status == 200) {
                _self.$refs[formName].resetFields();
                _self.userList();
              } else {
                _self.$message({
                  type: "info",
                  message: "res.meta.msg",
                });
              }
            });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else {
        let _self = this;
        let { id, name, startTime, endTime } = _self.ruleForm;
        let data = {
          id,
          name,
          startTime,
          endTime,
        };
        editSes(data).then((res) => {
          console.log(res);
          if (res.meta.status == 200) {
            _self.userList();
            _self.ruleForm = {};
          }
        });
      }
    },
  },
  components: { SupInput, Suptable },
};
</script>
<style scoped>
#shuju {
  padding: 20px;
  margin: 20px 0;
  border: 1px solid #ebeef5;
  border-radius: 10px;
}
.right {
  float: right;
}
.clear {
  clear: both;
}
</style>